package com.ossean.dao;

import java.util.Set;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ossean.model.OpenSourceProject;
import com.ossean.model.Taggings;



public interface DBDest {
    //从别名表中找出和synonym相同的别名对应的ID
	@Select("select distinct(prjId) from ${targetTable} where synonyms=#{Synonym} and flag = #{flag}")
	public Set<Integer> selectSameSynonymPrj(@Param("targetTable")String targetTable, @Param("Synonym")String Synonym,@Param("flag") int flag);
	//根据别名和项目Id更新别名表中的flag
	@Update("update ${targetTable} set flag = #{flag} where synonyms = #{synonym} and prjId != #{prjId}")
	public void updateSynoymmings(@Param("targetTable") String targetTable,@Param("synonym") String synonym,@Param("prjId") int prjId,@Param("flag")int flag);
	
	//删除open_source_projects表中对应id的数据
	@Delete("delete from ${table} where id=#{id}")
	public void deleteOpenSourceProjectsItem(@Param("table") String table, @Param("id") int id);
	
	//查找open_source_projects表对应id的记录
	@Select("select * from ${table} where id=#{id}")
	public OpenSourceProject selectOpenSourceProjectsItem(@Param("table") String table, @Param("id") int id);
	
	//查找open_source_projects表对应id的记录
	@Select("select id from ${table}")
	public Set<Integer> selectOpenSourceProjectsIds(@Param("table") String table);
	
	
	/**
	 * 下面是transferProjects程序的函数
	 */
	//向open_source_projects表中插入对象数据
	@Insert("insert into ${table} (id,name,description,"
			+ "url,url_md5,language,category,homepage,license,"
			+ "source,created_time,updated_time,extracted_time,"
			+ "tags,tags_for_search,synonyms,update_mark,filtration) values (#{model.id},#{model.name},#{model.description},"
			+ "#{model.url},#{model.url_md5},#{model.language},"
			+ "#{model.category},#{model.homepage},#{model.license},#{model.source},"
			+ "#{model.created_time},#{model.updated_time},#{model.extracted_time},#{model.tags},"
			+ "#{model.tags_for_search},#{model.synonyms},#{model.update_mark},#{model.filtration})")
	public void insertOsp(@Param("table") String table, @Param("model") OpenSourceProject model);
		
	//查找刚刚插入open_source_projects表中的记录id
	@Select("select id from ${table} where name=#{model.name} order by id desc limit 1")
	public int getAutoIncrementOspId(@Param("table") String table, @Param("model") OpenSourceProject model);
	
	//插入tag
	@Insert("insert ignore into ${table} (name) values (#{name})")
	public void insertTag(@Param("table") String table, @Param("name") String name);
	
	//根据tag name查找id
	@Select("select id from ${table} where name=#{name}")
	public int selectTagIdByName(@Param("table") String table, @Param("name") String name);
	
	//插入tagging
	@Insert("insert ignore into ${table} (tag_id,taggable_id,taggable_type,context,created_at,disagree_num) values (#{model.tag_id},#{model.taggable_id},#{model.taggable_type},#{model.context},#{model.created_at},#{model.disagree_num})")
	public void insertTagging(@Param("table") String table, @Param("model") Taggings model);
	
	//删除对应ospId的匹配结果
	@Delete("delete from ${table} where osp_id = ${ospId}")
	public void deleteMatchResult(@Param("ospId") int ospId, @Param("table") String table);
	
	//有相同别名的项目数量
	@Select("select count(*) from ${table} where synonyms like '%,${name},%' ")
	public int selectPrjNumFromOSP(@Param("table") String table, @Param("name") String name);
}